package com.dataStatistics.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataStatistics.clinet.MemberClient;
import com.dataStatistics.clinet.*;
import com.dataStatistics.entity.*;
import com.dataStatistics.service.IStatisticsDailyService;
import com.dataStatistics.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-06-07
 */
@CrossOrigin
@RestController
@RequestMapping("/statisticsDaily")
public class StatisticsDailyController {
    @Autowired
    private IStatisticsDailyService iStatisticsDailyService;

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private MemberClient memberClient;



    /**
     * 插入更新的数据
     * */

    @GetMapping("/statistics/{day}")
    public R statisticsToday(@PathVariable("day")String day){
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        R buyCountToday = orderClient.getBuyCountToday(day);
        HashMap<String, Object> data = buyCountToday.getData();
        Map todayMap = (Map) data.get("todayMap");
        R registerCount = memberClient.getRegisterCount(day);
        HashMap<String, Object> registerCountData = registerCount.getData();
        Integer Rcount = (Integer)registerCountData.get("registerCount");
        Integer count = (Integer)todayMap.get("total");
        return R.ok().msg("数据已更新").data("todayMap",todayMap).data("registerCount",Rcount);
    }


    /**
     * 定时任务
     * 每天凌晨一点执行该任务
     * */
    @Scheduled(cron = "0 0 1 * * ?")
    public void updateStatisticsMessage(){
        DateTime yesterday = DateUtil.yesterday();
        String day = DateUtil.formatDate(yesterday);
        iStatisticsDailyService.updateStatisticsData(day);
    }

    /**
     *
     * 显示今日的数据
     *
     *
     * */
    @GetMapping("/showYedterdayData")
    public R getYedterdayData(){
        DateTime yesterday = DateUtil.yesterday();
        String day = DateUtil.formatDate(yesterday);
        System.out.println(day);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        StatisticsDaily one = iStatisticsDailyService.getOne(queryWrapper);
        return R.ok().data("data",one);
    }


    /**
     *
     *根据统计因子进行统计
     * 由于Echart 的要求，数据是需要以json数组来呈现的
     *
     * */
    @GetMapping("dataStatisticsByDivisor/{divisor}/{start}/{end}")
    public R dataStatisticsByDivisor(@PathVariable("divisor")String divisor,
                                     @PathVariable("start")String start,
                                     @PathVariable("end")String end){

        Map<String,Object> dataMap = iStatisticsDailyService.dataStatisticsByDivisor(divisor,start,end);
        return R.ok().data("dataMap",dataMap);

    }


}
